/*
 * @Author: zhangguoliang
 * @Date: 2022-05-11 17:33:31
 * @LastEditors: sunchen
 * @LastEditTime: 2023-06-08 14:58:35
 */
import { defineStore } from 'pinia';
import Cookies from 'js-cookie';

export const useUserStore = defineStore(
  'userStore',
  {
    state: () => ({
      info: {
        // 门户传过来的数据
        name: '',
      },
      userInfo: {
        sequenceNbr: '',
        agencyCode: '',
      },
    }),
    actions: {
        // 1,手动，2自动，token过期
      logout(type = 2){
        console.log('调用了退出 ',type,this.info)
        if(this.info.logout){
          Cookies.remove('pricing_token');
          sessionStorage.clear()
          this.info.logout(type);
          this.REMOVE_USERINFO();
        }
      },
      SET_INFO(info) {
        this.info = info;
      },
      /**
       * @description: 获取用户信息
       * @return {*}
       */
      SET_USERINFO(info) {
        this.userInfo = info;
      },
      REMOVE_USERINFO() {
        this.userInfo = {
          sequenceNbr: '',
          agencyCode: '',
        };
        this.info = {
          name: '',
        };
      },
    },
    getters: {
      getName: (state) => state.info.name,
      getUserSequenceNbr: (state) => state.userInfo.sequenceNbr,
    },
    persist: {
      enabled: true, // true 表示开启持久化保存,
      strategies: [
        {
          key: 'user',
          storage: localStorage,
        },
      ],
    }
  }
);
